class Solution
{
public:
    int findTheLongestBalancedSubstring(string s)
    {
        int n = s.size();
        int maxLen = 0;
        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j < n; j += 2)
            {
                int len = j - i + 1;
                bool valid = true;
                for (int k = 0; k < len / 2; ++k)
                {
                    if (s[i + k] != '0')
                    {
                        valid = false;
                        break;
                    }
                }
                if (!valid)
                {
                    continue;
                }
                for (int k = len / 2; k < len; ++k)
                {
                    if (s[i + k] != '1')
                    {
                        valid = false;
                        break;
                    }
                }
                if (valid)
                {
                    maxLen = max(maxLen, len);
                }
            }
        }
        return maxLen;
    }
};